package com.sun.androidexerciseproject.function.util.greendao;

import android.content.Context;

import com.sun.androidexerciseproject.function.manager.DaoManager;
import com.sun.androidexerciseproject.function.model.greendao.MsgModel;
import com.sun.androidexerciseproject.function.model.greendao.MsgModelDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class MsgModelDaoUtils {
    private static final String TAG = MsgModelDaoUtils.class.getSimpleName();
    private DaoManager daoManager;

    public MsgModelDaoUtils(Context context) {
        daoManager = DaoManager.getInstance();
    }

    /**
     * 完成msgModel记录的插入，如果表未创建，先创建msgModel表
     *
     * @return
     */
    public boolean insertMsgModel(MsgModel msgModel) {
        return daoManager.getDaoSession().getMsgModelDao().insertOrReplace(msgModel) == -1 ? false : true;
    }

    /**
     * 插入多条数据，在子线程操作
     *
     * @param msgModelList
     * @return
     */
    public boolean insertMultMsgModel(final List<MsgModel> msgModelList) {
        boolean flag = false;
        try {
            daoManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (MsgModel msgModel : msgModelList) {
                        daoManager.getDaoSession().insertOrReplace(msgModel);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     *
     * @param msgModel
     * @return
     */
    public boolean updateMsgModel(MsgModel msgModel) {
        boolean flag = false;
        try {
            daoManager.getDaoSession().update(msgModel);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     *
     * @param msgModel
     * @return
     */
    public boolean deleteMsgModel(MsgModel msgModel) {
        boolean flag = false;
        try {
            //按照id删除
            daoManager.getDaoSession().delete(msgModel);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     *
     * @return
     */
    public boolean deleteAll() {
        boolean flag = false;
        try {
            //按照id删除
            daoManager.getDaoSession().deleteAll(MsgModel.class);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public List<MsgModel> queryAllMsgModel() {
        return daoManager.getDaoSession().loadAll(MsgModel.class);
    }

    /**
     * 根据主键id查询记录
     *
     * @param id
     * @return
     */
    public MsgModel queryMsgModelById(long id) {
        return daoManager.getDaoSession().load(MsgModel.class, id);
    }

    /**
     * 使用queryBuilder进行查询
     *
     * @return
     */
    public MsgModel queryMsgModelByQueryBuilder(long id) {
        try {
            QueryBuilder<MsgModel> queryBuilder = daoManager.getDaoSession().queryBuilder(MsgModel.class);
            return queryBuilder.where(MsgModelDao.Properties.Id.eq(id)).unique();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}